package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhidao.dao.CommentsMapper;
import com.example.zhidao.model.Spage.ShowPage;
import com.example.zhidao.model.po.Comments;
import com.example.zhidao.model.po.Fans;
import com.example.zhidao.model.po.User;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.model.vo.CommentVo;
import com.example.zhidao.service.CommentsService;
import com.example.zhidao.service.UserService;
import com.example.zhidao.utils.Ujwt;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.print.Pageable;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/comments")
public class CommentsController {//3
    @Autowired
    CommentsService commentsService;

    @Autowired
    UserService userService;

    @Autowired
    Ujwt ujwt;


    //    新增一级评论
    @PostMapping("/one")
    public ResponseData insertcommentone(@RequestHeader("token") String token,@RequestBody Comments comments){

//        解token
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();

        comments.setUid(uid);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        comments.setId(uuid);
        Date date=new Date();
        comments.setCreatetime(date);
        comments.setType(1);
        comments.setTopid(null);
        comments.setGood(0);
        comments.setUpuser(null);

//        采取事务
        int i=commentsService.onecomment(comments);

        if(i==1){
            return new ResponseData(i,true,"评论成功",200);
        }else{
            return new ResponseData(i,false,"评论失败",201);
        }
    }


    //    新增二级评论
    @PostMapping("/two")
    public ResponseData insertcommenttwo(@RequestHeader("token") String token,@RequestBody Comments comments){

        //        解token
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();

        comments.setUid(uid);

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        comments.setId(uuid);
        Date date=new Date();
        comments.setCreatetime(date);
        comments.setType(2);
        comments.setGood(0);

        int i=commentsService.onecomment(comments);

        if(i==1){
            return new ResponseData(i,true,"评论成功",200);
        }else{
            return new ResponseData(i,false,"评论失败",201);
        }
    }

    @Resource
    CommentsMapper commentsMapper;

//    @GetMapping("/aa")
//    public ResponseData tetx(String bid,@RequestParam(defaultValue = "1") Integer pageNum,
//                             @RequestParam(defaultValue = "10") Integer pageSize) {
//        System.out.println("1111111111111111111111111111111111111111111111111111111111");
//        System.out.println(pageNum);
//        System.out.println(pageSize);
//
////        对数据进行处理
//        int pageno=pageNum*pageSize;
//
//        List<CommentVo> result=new ArrayList<>();
//        List<CommentVo> commentVoList = commentsMapper.one(bid,pageNum,pageSize);
//        for(CommentVo cv: commentVoList){
//            List<CommentVo> commentVoList1=commentsMapper.seltwo(bid,cv.getId());
//            cv.setChildren(commentVoList1);
//            result.add(cv);
//        }
//
//        if(result!=null){
//
//            return new ResponseData(result,true,"成功",200);
//        }else{
//            return new ResponseData(null,false,"失败",201);
//        }
//
//    }



    @GetMapping()
    public ResponseData tetx1(String bid,String mid,@RequestParam(defaultValue = "1") Integer page ,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Comments> qw=new QueryWrapper<>();
        qw.eq("bid",bid);
        qw.eq("type",1);
        showPage.setSize(commentsService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));




        List<CommentVo> result=new ArrayList<>();
        List<CommentVo> commentVoList = commentsMapper.one(bid,mid,pageBegin,pageSize);
        for(CommentVo cv: commentVoList){
            List<CommentVo> commentVoList1=commentsMapper.seltwo(bid,cv.getId(),mid);
            cv.setChildren(commentVoList1);
            result.add(cv);
        }
        showPage.setData(result);
        if(result!=null){

            return new ResponseData(showPage,true,"成功",200);
        }else{
            return new ResponseData(null,false,"失败",201);
        }

    }





}
